//
//  WmcUserAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/3/29.
//

import Foundation

struct WmcUserCenterAPI {
    /// 统一用户同步信息接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193939
    /// - Parameters:
    ///   - userInfo: 用户信息
    /// - Returns: 账号信息model
    static func fetchAccountInfo(userInfo: [String: String]) -> Observable<WmcAccountModel> {
        return WmcNetworkRequest.requestData(path: "/api/member/verify") { request in
            request.method = .get
            request.extraHeaderFields = userInfo
        }.parseModelWithClass(WmcAccountModel.self)
    }

    
    /// 统一用户同步信息接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193939
    /// - Parameters:
    ///   - userInfo: 用户信息
    ///   - success: 请求成功
    ///   - failure: 请求失败
    static func fetchAccountInfo(userInfo: [String: String], success: @escaping ([String: Any]) -> Void, failure: @escaping (YPNetworkError?) -> Void) {
        return WmcNetworkRequest.requestData(path: "/api/member/verify") { request in
            request.method = .get
            request.extraHeaderFields = userInfo
        } success: { (response: YPNetworkResponse) in
            if let data = response.data as? [String: Any] {
                success(data)
            } else {
                failure(nil)
            }
        } failure: { error in
            failure(error)
        }
    }

    /// 用户个人详情获取接口
    /// documentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193948
    /// - Returns: 用    户信息model
    static func fetchUserInfo() -> Observable<WmcUserInfoModel> {
        return WmcNetworkRequest.requestData(path: "/api/member/detail") { request in
            request.method = .get
        }.parseModelWithClass(WmcUserInfoModel.self)
    }

    /// 更新名字
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193962
    /// - Parameter name: 名字
    /// - Returns: response
    static func updateNickname(name: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["type": "name", "name": name]
        return WmcNetworkRequest.requestData(path: "/api/member/update", parameters: params) { request in
            request.method = .post
        }
    }

    /// 更新手机号
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193962
    /// - Parameters:
    ///   - phone: 手机号
    ///   - verificationCode: 验证码
    /// - Returns: response
    static func updatePhone(phone: String, verificationCode: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["type": "phone", "phone": phone, "code": verificationCode]
        return WmcNetworkRequest.requestData(path: "/api/member/update", parameters: params) { request in
            request.method = .post
        }
    }

    /// 修改密码
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193962
    /// - Parameters:
    ///   - password: 密码
    ///   - confirmPassword: 确认输入密码
    ///   - oldPassword: 旧密码
    /// - Returns: response
    static func updatePassword(password: String, confirmPassword: String, oldPassword: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["type": "pwd", "pwd_type": 2, "pwd": password, "pwd_confirm": confirmPassword, "old_pwd": oldPassword]
        return WmcNetworkRequest.requestData(path: "/api/member/update", parameters: params) { request in
            request.method = .post
        }
    }

    /// 第一次设置密码
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2193962
    /// - Parameters:
    ///   - password: 密码
    ///   - confirmPassword: 确认输入密码
    /// - Returns: response
    static func setPassword(password: String, confirmPassword: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["type": "pwd", "pwd_type": 1, "pwd": password, "pwd_confirm": confirmPassword]
        return WmcNetworkRequest.requestData(path: "/api/member/update", parameters: params) { request in
            request.method = .post
        }
    }

    /// 发送换绑手机验证码
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498344
    /// - Parameter phone: 手机号
    /// - Returns: response
    static func sendVerificationCode(phone: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["phone": phone]
        return WmcNetworkRequest.requestData(path: "/api/code/send", parameters: params) { request in
            request.method = .post
        }
    }
    
    static func loginYupaoAccount(host: String, phone: String, password: String, success: @escaping ([String: Any]) -> Void, failure: @escaping (YPNetworkError?) -> Void) {
        let params: [String: Any] = ["tel": phone, "password": password]
        
        WmcNetworkRequest.requestData(path: "/login/mobile/pwdLogin", parameters: params) { request in
            request.redirectHost = host
        } success: { (response: YPNetworkResponse) in
            if let data = response.data as? [String: Any] {
                success(data)
            } else {
                failure(nil)
            }
        } failure: { error in
            failure(error)
        }
    }
    
    static func fetchYupaoAccountInfo(host: String, token: String, isNewUser: Bool, origin: String, success: @escaping ([String: Any]) -> Void, failure: @escaping (YPNetworkError?) -> Void) {
        let params: [String: Any] = ["token": token, "isNewUser": isNewUser, "origin": origin]
        WmcNetworkRequest.requestData(path: "/job/member/init", parameters: params) { request in
            request.redirectHost = host
        } success: { (response: YPNetworkResponse) in
            if let data = response.data as? [String: Any] {
                success(data)
            } else {
                failure(nil)
            }
        } failure: { error in
            failure(error)
        }
    }
}
